package com.itheima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOPAdvice {
    @Pointcut("execution(* *(..))")
    public void pt(){

    }

   /* //获取参数
    @Before("pt()")
    public void before(JoinPoint jp){
        Object[] args = jp.getArgs();
        System.out.println("before"+args[0]);
    }*/


//    @After("pt()")
//    public void after(){
//        System.out.println("after");
//    }


    //获取返回值
    @AfterReturning(pointcut = "pt()",returning = "ret")
    public void afterReturning(Object ret){
        System.out.println("afterReturning"+ret);
    }


    //获取异常
    @AfterThrowing(pointcut = "pt()",throwing ="t")
    public void afterThrowing(Throwable t){
        System.out.println("afterThrowing"+t.getMessage());
    }


   /* //获取返回值
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object ret = pjp.proceed();
        System.out.println("around"+ret);
        return ret;
    }*/
    //获取异常
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp)  {
        Object ret = null;
        try {
            ret = pjp.proceed();
        } catch (Throwable throwable) {
            System.out.println("异常处理了"+throwable.getMessage());
        }
        System.out.println("around"+ret);
        return ret;
    }
}
